package org.x.user.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.lang.Boolean;
import java.lang.String;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.x.common.base.result.Result;
import org.x.user.entity.SysPerm;
import org.x.user.entity.SysRole;
import org.x.user.entity.SysRolePerm;
import org.x.user.dto.CreateGroup;
import org.x.user.dto.DeleteGroup;
import org.x.user.dto.PermDTO;
import org.x.user.dto.RoleDTO;
import org.x.user.dto.RolePermDTO;
import org.x.user.dto.UpdateGroup;
import org.x.user.query.PermQuery;
import org.x.user.query.QueryGroup;
import org.x.user.service.PermService;

/**
 * 权限管理
 *
 * @author 夏包包
 * @date 2025-03-08 13:39:03
 */
@Tag(name = "权限管理")
@RequestMapping("permission")
@RestController
public class PermissionController {

    @Autowired
    private PermService permissionService;

    /**
     * 新增权限
     *
     * @param permDTO 权限管理DTO
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Operation(summary = "新增权限")
    @ResponseBody
    public Result<Boolean> addPerm(@RequestBody @Validated(CreateGroup.class) PermDTO permDTO) {
        Boolean result = permissionService.addPerm(permDTO);
        return Result.success(result);
    }

    /**
     * 删除权限
     *
     * @param permQuery 权限管理查询对象
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @Operation(summary = "删除权限")
    @ResponseBody
    public Result<Boolean> deletePerm(@RequestBody @Validated(DeleteGroup.class) PermQuery permQuery) {
        Boolean result = permissionService.deletePerm(permQuery);
        return Result.success(result);
    }

    /**
     * 修改权限
     *
     * @param permDTO 权限管理DTO
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @Operation(summary = "修改权限")
    @ResponseBody
    public Result<Boolean> updatePerm(@RequestBody @Validated(UpdateGroup.class) PermDTO permDTO) {
        Boolean result = permissionService.updatePerm(permDTO);
        return Result.success(result);
    }

    /**
     * 查询权限
     *
     * @param permId
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @Operation(summary = "查询权限")
    public Result<SysPerm> getPerm(@Validated(QueryGroup.class) String permId) {
        SysPerm result = permissionService.getPerm(permId);
        return Result.success(result);
    }

    /**
     * 新增角色
     *
     * @param roleDTO 角色管理DTO
     * @return
     */
    @RequestMapping(value = "/role/add", method = RequestMethod.POST)
    @Operation(summary = "新增角色")
    @ResponseBody
    public Result<Boolean> addRole(@RequestBody @Validated(CreateGroup.class) RoleDTO roleDTO) {
        Boolean result = permissionService.addRole(roleDTO);
        return Result.success(result);
    }

    /**
     * 删除角色
     *
     * @param roleDTO 角色管理DTO
     * @return
     */
    @RequestMapping(value = "/role/delete", method = RequestMethod.DELETE)
    @Operation(summary = "删除角色")
    @ResponseBody
    public Result<Boolean> deleteRole(@RequestBody @Validated(DeleteGroup.class) RoleDTO roleDTO) {
        Boolean result = permissionService.deleteRole(roleDTO);
        return Result.success(result);
    }

    /**
     * 修改角色
     *
     * @param roleDTO 角色管理DTO
     * @return
     */
    @RequestMapping(value = "/role/update", method = RequestMethod.PUT)
    @Operation(summary = "修改角色")
    @ResponseBody
    public Result<Boolean> updateRole(@RequestBody @Validated(UpdateGroup.class) RoleDTO roleDTO) {
        Boolean result = permissionService.updateRole(roleDTO);
        return Result.success(result);
    }

    /**
     * 查询角色
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/role/info", method = RequestMethod.GET)
    @Operation(summary = "查询角色")
    public Result<SysRole> getRole(@Validated(QueryGroup.class) String roleId) {
        SysRole result = permissionService.getRole(roleId);
        return Result.success(result);
    }

    /**
     * 新增角色权限分配
     *
     * @param rolePermDTO 角色权限分配DTO
     * @return
     */
    @RequestMapping(value = "/role/perm/add", method = RequestMethod.POST)
    @Operation(summary = "新增角色权限分配")
    @ResponseBody
    public Result<Boolean> addRolePerm(@RequestBody @Validated(CreateGroup.class) RolePermDTO rolePermDTO) {
        Boolean result = permissionService.addRolePerm(rolePermDTO);
        return Result.success(result);
    }

    /**
     * 删除角色权限分配
     *
     * @param rolePermDTO 角色权限分配DTO
     * @return
     */
    @RequestMapping(value = "/role/perm/delete", method = RequestMethod.DELETE)
    @Operation(summary = "删除角色权限分配")
    @ResponseBody
    public Result<Boolean> deleteRolePerm(@RequestBody @Validated(DeleteGroup.class) RolePermDTO rolePermDTO) {
        Boolean result = permissionService.deleteRolePerm(rolePermDTO);
        return Result.success(result);
    }

    /**
     * 修改角色权限分配
     *
     * @param rolePermDTO 角色权限分配DTO
     * @return
     */
    @RequestMapping(value = "/role/perm/update", method = RequestMethod.PUT)
    @Operation(summary = "修改角色权限分配")
    @ResponseBody
    public Result<Boolean> updateRolePerm(@RequestBody @Validated(UpdateGroup.class) RolePermDTO rolePermDTO) {
        Boolean result = permissionService.updateRolePerm(rolePermDTO);
        return Result.success(result);
    }

    /**
     * 查询角色权限分配
     *
     * @param rolePermId
     * @return
     */
    @RequestMapping(value = "/role/perm/info", method = RequestMethod.GET)
    @Operation(summary = "查询角色权限分配")
    public Result<SysRolePerm> getRolePerm(@Validated(QueryGroup.class) String rolePermId) {
        SysRolePerm result = permissionService.getRolePerm(rolePermId);
        return Result.success(result);
    }
}
